//
//  LBCCategoryListViewController.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryListViewController.h"

@interface LBCCategoryListViewController ()<UIScrollViewDelegate, CategoryChooseDelegate>

@property (nonatomic, strong)UIScrollView *smallScrollV;
@property (nonatomic, strong)UIScrollView *bigScrollV;
@property (nonatomic, strong)NSMutableArray *tagArr;
@property (nonatomic, strong)UIView *corV;
@property (nonatomic, assign)NSInteger currTag;
@property (nonatomic, strong)UIView *smallSubV;

@end

@implementation LBCCategoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 取消渲染 让左右两边的按钮变色
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.969 green:0.427 blue:0.071 alpha:1.000];
    
    self.navigationItem.title = _category.title;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_back_h@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.tagArr = [NSMutableArray array];
// 标签请求数据
    [self getTagData];
    
    self.smallScrollV = [UIScrollView new];
    _smallScrollV.showsHorizontalScrollIndicator = NO;
    _smallScrollV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
    [self.view addSubview:_smallScrollV];
    [_smallScrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(@0);
        make.height.mas_equalTo(@40);
        make.right.mas_equalTo(-SCREEN_WIDTH / 5);
        
    }];
    
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    // 取消按钮点击效果
    moreBtn.adjustsImageWhenHighlighted = NO;
    moreBtn.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
    [moreBtn addTarget:self action:@selector(moreButton) forControlEvents:UIControlEventTouchUpInside];
    [moreBtn setImage:[UIImage imageNamed:@"seg_pulldown_n@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.left.equalTo(_smallScrollV.mas_right);
        make.bottom.equalTo(_smallScrollV.mas_bottom);
        
    }];
    
    self.bigScrollV = [UIScrollView new];
    _bigScrollV.delegate = self;
    _bigScrollV.pagingEnabled = YES;
    _bigScrollV.showsHorizontalScrollIndicator = NO;
    _bigScrollV.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [self.view addSubview:_bigScrollV];
    [_bigScrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(_smallScrollV.mas_bottom);
        
    }];
    
}



- (void)getTagData{
    [AFNetworkTool getUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=%@&device=iPhone", _categoryId, _contentType] body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LBCCategoryTag *cateTag = [LBCCategoryTag new];
            [cateTag setValuesForKeysWithDictionary:dic];
            [_tagArr addObject:cateTag];
        }
        LBCCategoryTag *tag = [LBCCategoryTag new];
        tag.tname = @"推荐";
        [_tagArr insertObject:tag atIndex:0];
        [self makeTagButton];
        [self makeContentList];
    } failure:^(NSError *error) {
        
        
    }];
}
// 创建标签按钮
- (void)makeTagButton{
    self.smallSubV = [UIView new];
    _smallSubV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
    [_smallScrollV addSubview:_smallSubV];
    [_smallSubV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_smallScrollV);
        make.height.equalTo(_smallScrollV);
        
    }];

    UIButton *lastBtn = nil;
    for (int i = 0; i < _tagArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000 + i;
        // 计算文字大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGFloat length = [[_tagArr[i] tname] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        [btn setTitle:[_tagArr[i] tname] forState:UIControlStateNormal];
        if (btn.tag == 1000) {
            [btn setTitleColor:[UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1] forState:UIControlStateNormal];
        } else {
            [btn setTitleColor:[UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1] forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_smallSubV addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_smallScrollV).offset(10);
            make.bottom.equalTo(_smallScrollV).offset(-10);
            make.width.mas_equalTo(length + 15);
            if (lastBtn) {
                make.left.mas_equalTo(lastBtn.mas_right).offset(25);
            } else {
                make.left.mas_equalTo(20);
            }
            
        }];
        lastBtn = btn;
        UIView *corView = [UIView new];
        corView.tag = 2000 + i;
        if (corView.tag == 2000) {
            corView.backgroundColor = [UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1];
        } else {
            corView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
        }
        [_smallSubV addSubview:corView];
        
        NSDictionary *attributes1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGFloat length1 = [lastBtn.titleLabel.text boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes1 context:nil].size.width;
        [corView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_smallSubV);
                make.height.mas_equalTo(@2);
                make.width.mas_equalTo(length1 + 5);
                make.left.equalTo(lastBtn.mas_left).offset(5);
            }];
        
    }
    [_smallSubV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(lastBtn.mas_right).offset(10);
        
    }];
    
    

}

- (void)makeContentList{
    UIView *bigSubV = [UIView new];
    bigSubV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
    [_bigScrollV addSubview:bigSubV];
    [bigSubV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_bigScrollV);
        make.height.equalTo(_bigScrollV);
        
    }];
    LBCCategoryHomeVC *cateHomeVC = [LBCCategoryHomeVC new];
    cateHomeVC.categoryId = _categoryId;
    cateHomeVC.contentType = _contentType;
    [bigSubV addSubview:cateHomeVC.view];
    // 当tableView点击就消失的时候,加这行代码
    [self addChildViewController:cateHomeVC];
    [cateHomeVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(_bigScrollV);
        make.width.equalTo(_bigScrollV);
        
    }];
    UIView *lastV = nil;
    for (int i = 1; i < _tagArr.count; i++) {
        LBCCategoryListVC *cateListVC = [LBCCategoryListVC new];
        cateListVC.cateTag = _tagArr[i];
        [bigSubV addSubview:cateListVC.view];
        [self addChildViewController:cateListVC];
        [cateListVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_bigScrollV);
            make.width.equalTo(_bigScrollV);
            if (lastV) {
                make.left.equalTo(lastV.mas_right);
            } else {
                make.left.equalTo(cateHomeVC.view.mas_right);
            }
            
        }];
        lastV = cateListVC.view;
    }
    [bigSubV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastV.mas_right);
        
    }];

    
}


// 标签按钮
- (void)btnAction:(UIButton *)btn{
    for (int i = 0; i < _tagArr.count; i++) {
        UIButton *button = [self.view  viewWithTag:i + 1000];
        UIView *lineV = [self.view viewWithTag:i + 2000];
        if (button.tag == btn.tag) {
            [button setTitleColor:[UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1];
        } else {
            [button setTitleColor:[UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
        }
    }
    _bigScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * (btn.tag % 1000), 0);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat tmp = _bigScrollV.contentOffset.x / SCREEN_WIDTH;
    for (int i = 0; i < _tagArr.count; i++) {
        UIButton *button = [self.view  viewWithTag:i + 1000];
        UIView *lineV = [self.view viewWithTag:2000 + i];
        if (button.tag == tmp + 1000) {
            if (tmp > 1 && tmp != _tagArr.count) {
                [UIView animateWithDuration:0.5 animations:^{
                    UIButton *btn = [self.view  viewWithTag:i + 1000 - 1];
                    _smallScrollV.contentOffset = CGPointMake(btn.frame.origin.x + 20, 0);
                }];
            }else if(tmp != _tagArr.count){
                [UIView animateWithDuration:0.5 animations:^{
                    _smallScrollV.contentOffset = CGPointMake(0, 0);
                    
                }];
            }
            [button setTitleColor:[UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1];
            
        } else {
            [button setTitleColor:[UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
        }
    }
}

// 轻拍手势
- (void)moreButton{
    LBCCategoryChooseV *cateChooseV = [LBCCategoryChooseV new];
    cateChooseV.categoryChooseDelegate = self;
    [self.view addSubview:cateChooseV];
    cateChooseV.tagArr = _tagArr;
    // 下拉动画
    [UIView animateWithDuration:0.5 animations:^{
        cateChooseV.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        cateChooseV.bigScrollV = _bigScrollV;
        cateChooseV.smallScrollV = _smallScrollV;
    }];
}

-(void)presentToListViewController:(NSInteger)index{
    for (int i = 0; i < _tagArr.count; i++) {
        UIButton *button = [self.view  viewWithTag:i + 1000];
        UIView *lineV = [self.view viewWithTag:2000 + i];
        if (button.tag == index + 1000) {
            [button setTitleColor:[UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.97 green:0.31 blue:0 alpha:1];
            
        } else {
            [button setTitleColor:[UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1] forState:UIControlStateNormal];
            lineV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.97 alpha:1];
        }
    }
}


// 返回按钮
- (void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
